"""
@name : 7-2.多进程-进程池-process-比较
@author : wenyao
@projectname: sanchuanglianxi
"""
#计算密集型，查看输出时间。
#进程池使用
from multiprocessing import Pool, current_process
import time
import os
def task(i):
    print(os.getpid())
    li = []
    for i in range(10**3):
       li.append(i)
if __name__ == "__main__":
    #多进程建议设置与cpu核数一致
    p = Pool(processes=4)
    pool_lst = []
    for i in range(2000):
        p.apply_async(func = task,args=(i,))
    #池子不再接收任务
    p.close()
    #等待子进程执行完毕后 再关闭进程池
    p.join()
#=========================================================
from multiprocessing import Pool, current_process, Process
import time
import os
def task(i):
    print(os.getpid())
    li = []
    for i in range(10**3):
       li.append(i)
if __name__ == "__main__":
    #多进程建议设置与cpu核数一致
    p_lst = []
    for i in range(2000):
        p = Process(target = task, args = (i,))
        p.start()
        p_lst.append(p)
    [p.join() for p in p_lst ]
#=========================================================================


#io密集型
#进程池
from multiprocessing import Pool, current_process
import time
import os
def task(i):
    print(os.getpid())
    time.sleep(1)
if __name__ == "__main__":
    #多进程建议设置与cpu核数一致
    p = Pool(processes=4)
    pool_lst = []
    for i in range(20):
        p.apply_async(func = task,args=(i,))
    #池子不再接收任务
    p.close()
    #等待子进程执行完毕后 再关闭进程池
    p.join()


#===========================================
from multiprocessing import Pool, current_process, Process
import time
import os
def task(i):
    print(os.getpid())
    time.sleep(1)
if __name__ == "__main__":
    #多进程建议设置与cpu核数一致
    p_lst = []
    for i in range(20):
        p = Process(target = task, args = (i,))
        p.start()
        p_lst.append(p)
    [p.join() for p in p_lst ]
